package team.jaman.autosell.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import team.jaman.autosell.entity.Manager;
/**
 * 登录拦截器
 * @author Administrator
 *
 */
public class LoginInterceptor implements HandlerInterceptor{
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
//		if(request.getSession().getAttribute("key").equals("szt")) {
//			return true;
//		} else {			
//			request.getRequestDispatcher("login.jsp").forward(request, response);
//			return false;
//		}
		//获取请求的URL
		StringBuffer url = request.getRequestURL();
		//URL:除了login.jsp是可以公开访问的，其余的URL都要进行拦截控制
		if(url.indexOf("/login.jsp")>0) {
			return true;
		}
		//获取session
		HttpSession session = request.getSession();
		Manager manager = (Manager)session.getAttribute("USER");
		//判断session里是不是有登录信息
		if(manager!=null) {
			return true;
		}
		
		request.getRequestDispatcher("login.jsp").forward(request, response);
		return false;
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
	}
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
	}
}
